<?php
/**
 * @title 211. 添加与搜索单词 - 数据结构设计
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class WordTree {
    public $val;
    public $isWord;
    public $next;

    public function __construct($val = null, $isWord = false, $next = [])
    {
        $this->val = $val;
        $this->isWord = $isWord;
        $this->next = $next;
    }
}

class WordDictionary {
    /**
     */
    function __construct() {
        $this->wordTree = new WordTree();
    }

    /**
     * @param String $word
     * @return NULL
     */
    function addWord($word) {
        /**
         * @since 2023-12-28 WordTree沿着next往下找word
         */
        $wordTree = $this->wordTree;
        for($i=0; $i<strlen($word); $i++){
            $char = $word[$i];
            if(!isset($wordTree->next[$char])){
                $wordTree->next[$char] = new WordTree($char);
            } else {}
            $wordTree = $wordTree->next[$char];
        }
        $wordTree->isWord = true;
    }

    /**
     * @param String $word
     * @return Boolean
     */
    function search($word) {
        /**
         * @since 2023-12-28 WordTree沿着next往下找word
         */
        $wordTreeArray = [$this->wordTree];
        for($i=0; $i<strlen($word); $i++){
            $char = $word[$i];

            /**
             * @since 2023-12-28 下次遍历WordTree集合
             */
            $nextWordTreeArray = [];

            /**
             * @since 2023-12-28 所有非空next，全部合并
             */
            if($char == "."){
                foreach ($wordTreeArray as $wordTree){
                    if(!empty($wordTree->next)){
                        $nextWordTreeArray = array_merge($nextWordTreeArray, array_values($wordTree->next));
                    } else {}
                }
            } else {
                foreach ($wordTreeArray as $wordTree){
                    if(isset($wordTree->next[$char])){
                        $nextWordTreeArray[] = $wordTree->next[$char];
                    } else {}
                }
            }

            /**
             * @since 2023-12-28 wordTree数组为空，无法继续匹配
             */
            if(empty($nextWordTreeArray)){
                return false;
            } else {
                $wordTreeArray = $nextWordTreeArray;
            }
        }

        /**
         * @since 2023-12-29 遍历wordTree，检查isWord
         */
        foreach ($wordTreeArray as $wordTree){
            if($wordTree->isWord){
                return true;
            } else {}
        }

        /**
         * @return Boolean
         */
        return false;
    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * $obj = WordDictionary();
 * $obj->addWord($word);
 * $ret_2 = $obj->search($word);
 */


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.28_8.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
